#include <stdio.h>
#include "ofc/DHTTPClient.h"

int main(int argc, char *argv[])
{
  DHTTPClient   *client = [DHTTPClient new];
  DURL          *url    = [DURL        new];
  DHashIterator *iter;

  [url url :"http://ofc.dvoudheusden.net/index.html"];

  printf("Send the request for a http page..\n");
  if (![client sendStartRequest :DHC_GET :url])  // send a GET for index.html
    printf("Error in sendStartRequest..\n");

  if (![client sendConnectionHeader :"close"])   // send that the connection should be closed
    printf("Error in sendConnectionHeader..\n");

  if (![client sendUserAgentHeader :"libofc"])   // send the user agent
    printf("Error in sendUserAgentHeader..\n");

  if (![client sendEndRequest])                  // send the end of the request
    printf("Error in sendEndRequest..\n");

  printf("Wait for the response of the server..\n");
  if ([client receiveReply])                     // wait for the response of the server
  {
    printf("Response reason:%d http version:%d.%d body length:%ld\n", 
      [client reason], [client peerMajor], [client peerMinor], [client bodyLength]);

    iter = [client headers];                     // print the headers in the reply

    if (iter != nil)
    {
      DText *value = [iter first];
    
      printf("Headers in reply:\n");
      while (value != nil)
      {
        DText *key = [iter key];

        printf("%s = %s\n", [key cstring], [value cstring]);

        value = [iter next];
      }
      [iter free];
    }
    else
      printf("No headers in reply..\n");

  }
  else
    printf("Error in receiveReply..\n");

  [client free];                       // Cleanup
  [url    free];

  return 0;
}

